var id = 0, tabulaireTable;
var libVisu = {enterKey:{init:function() {
  enterKeyCreation();
  enterKeyRestore();
  enterKeyBackup();
  enterKeyLog()
}}, varSelector:{init:function() {
  diagRecherche = $("#dialogRecherche");
  diagLog = $("#dialogLog");
  diagRestore = $("#dialogRestore");
  diagBackup = $("#dialogBackup");
  diagCreation = $("#dialogCreation");
  diagCommand = $("#dialogCommand");
}}, varTimer:{init:function() {
  timers.add("/visualisation/valideipresult", new Array(valideIpVisualisationResult, 10, 30));
  timers.add("/visualisation/lancerechercheresult", new Array(lanceRechercheResult, 15, 80));
  timers.add("/visualisation/vuetopologieresult", new Array(visualisationTopologieResult, 10, 80));
  timers.add("/creation/liensipresp", new Array(liensIpResp, 10, 80));
  timers.add("/log/logresp", new Array(logTabulaireResp, 20, 80));
  timers.add("/config/restoreresp", new Array(restoreResp, 20, 80));
  timers.add("/config/checkbackupresp", new Array(backupResp, 20, 80));
  timers.add("/console/commandresp", new Array(getCommand, 20, 80));
}}};
$(function() {
 libVisu.varSelector.init();
 setTimeout(libVisu.enterKey.init, 10);
 setTimeout(libVisu.varTimer.init, 20);
});
function dialogRecherche() {
  var btns = {};
  btns[BUTTON_CANCEL] = function() {$(this).dialog("close")};
  btns[BUTTON_SEARCH] = function() {$(this).dialog("close"); valideIpVisualisation()};
  $("#ip, #mask").val("");
  diagRecherche.dialog({autoOpen:false, draggable:true, width:388, modal:true, buttons:btns});
  diagRecherche.dialog("open")
}
function cleanTabVisu() {
  tabulaireTable.fnClearTable()
}
function initTabVisu() {
  tabulaireTable = $("#visuTab").dataTable({bFilter:false, bInfo:false, bAutoWidth:false, bLengthChange:false, bSort:false, bSortClasses:false, bSortable:false, oLanguage:{sZeroRecords:"&#216;"}, iDisplayLength:1000, aoColumns:[{sTitle:null, sType:"html", sClass:"center", sWidth:"2%"}, {sTitle:"id", sType:"number", bVisible:false}, {sTitle:VISU_TAB_HOSTNAME, sType:"string", sClass:"center", sWidth:"25%"}, {sTitle:VISU_TAB_IP, sType:"string", sClass:"center", sWidth:"25%"}, {sTitle:VISU_TAB_PORT, sType:"string", sClass:"center",
  sWidth:"5%"}, {sTitle:VISU_TAB_UPTIME, sType:"string", sClass:"center", sWidth:"20%"}, {sTitle:VISU_TAB_LIST, sType:"string", sClass:"center", sWidth:"25%"}, {sTitle:"Model", sType:"string", sWidth:"33%", bVisible:false}]})
}
function addRowTabulaire(a) {
  for(var b = "<input type='checkbox' name='" + id + "'value='" + id + "'/>", c = tabulaireTable.fnGetNodes(), d = 0;d < c.length;d++) {
    if(c[d].cells[2].textContent == a[1]) {
      updateRowTabulaire(c[d], a);
      return true
    }
  }
  var e = tabulaireTable.fnAddData([b, id, a[0], a[1], a[2], a[3], a[5], a[4]]);
  toolTipsRow(e, a[4]);
  id++;
  return true
}
function updateRowTabulaire(a, b) {
  aPos = tabulaireTable.fnGetPosition(a);
  tabulaireTable.fnUpdate(b[0].toString(), aPos, 2);
  tabulaireTable.fnUpdate(b[1].toString(), aPos, 3);
  tabulaireTable.fnUpdate(b[2].toString(), aPos, 4);
  tabulaireTable.fnUpdate(b[3].toString(), aPos, 5);
  tabulaireTable.fnUpdate(b[5].toString(), aPos, 6)
}
function toolTipsRow(a, b) {
  a = tabulaireTable.fnGetNodes(a);
  a.setAttribute("title", b);
  return true
}
function visuTabToolTip() {
  $("#visuTab tbody tr[title]").tooltip({delay:0, track:true, fade:250})
}
function arrayRow(g) {
  for(var a = [], b = [], c = tabulaireTable.fnGetNodes(), d = true, e = 0, f = c.length;e < f;e++) {
    if($(c[e]).hasClass("row_selected")) {
      d = false;
      a.push("[{" + c[e].cells[g].textContent + "}]");
      b = []
    }
    b.push("[{" + c[e].cells[g].textContent + "}]")
  }
  if(d == true) {
    a = b
  }
  return a
}
function visualisationTabulaireResp(a) {
  for(var b = 0;b < a.donnees.length;b++) {
    addRowTabulaire([a.donnees[b].host, a.donnees[b].ip, a.donnees[b].port, a.donnees[b].uptime, a.donnees[b].model, a.donnees[b].link])
  }
  return true
}
function selectRow() {
  if($(this).hasClass("row_selected")) {
    $(this).removeClass("row_selected");
    this.cells[0].childNodes[0].checked = false
  }else {
    $(this).addClass("row_selected");
    this.cells[0].childNodes[0].checked = true
  }
  return true
}
function valideIpVisualisation() {
  $.post(baseUrl + "/visualisation/valideip/", {ip:$("#ip").val(), mask:$("#mask").val()}, attenteReponse, "json")
}
function valideIpVisualisationResult(a) {
  a.valide == true && $.post(baseUrl + "/visualisation/lancerecherche", {ip:a.ip, mask:a.mask}, attenteReponse, "json");
  a.valide == false && alertGrowl(MISC_ERROR, VISU_SEARCH_ERROR);
  a.valide == "error" && alertGrowl(MISC_ERROR, VISU_SEARCH_ERROR2);
  return false
}
function recolteRechercheVisualisation(a) {
  visualisationTabulaireResp(a)
}
function lanceRechercheResultRelance(a) {
  $.post(baseUrl + "/visualisation/lancerechercheresult", {o:a}, attenteReponse, "json")
}
function lanceRechercheResult(a) {
  if (typeof a.donnees == 'undefined' && typeof a.rechercheTermine == 'undefined')
      return false;
  if (typeof a.donnees != 'undefined')
       recolteRechercheVisualisation(a);
  donneeARelancer(a) ? lanceRechercheResultRelance() : alertGrowl(VISU_SEARCH_TXT, VISU_SEARCH_END);
  $("#visuTab tr").click(selectRow);
  visuTabToolTip()
}
function enterKeyCreation() {
  $(":input", diagCreation).keypress(function(a) {
    if(a.which && a.which == 13 || a.keyCode && a.keyCode == 13) {
      diagCreation.dialog("close");
      creationMap();
      return false
    }
  })
}
function dialogCreation() {
  if(tabulaireTable == undefined) {
    alertGrowl(MISC_ERROR, VISU_ERROR_EMPTYSEARCH)
  } else {
  $("#nameCreation").val("");
  var btns = {};
  btns[BUTTON_CANCEL] = function() {$(this).dialog("close")};
  btns[BUTTON_CREATE] = function() {$(this).dialog("close");creationMap()};
  diagCreation.dialog({autoOpen:false, draggable:true, width:388, modal:true, buttons: btns});
  diagCreation.dialog("open")
  }
}
function liensIp() {
  var a = arrayRow(5);
  $.post(baseUrl + "/creation/liensip/", {name:name.val(), data:a.toString()}, attenteReponse, "json");
  return false
}
function liensIpResp(a) {
  visualisationTopologie(a.name);
  refreshTree()
}
function checkNameMap(a) {
  if(a.error == true) {
    alertGrowl(MISC_ERROR, VISU_MAP_EXIST);
    return false
  }
  liensIp();
  return true
}
function creationMap() {
   name = $("#nameCreation");
  if(name.val().length == 0) {
    alertGrowl(MISC_ERROR, VISU_MAP_EMPTY);
    return false
  }
  if(name.val().length > 10) {
    alertGrowl(MISC_ERROR, VISU_MAP_LONG);
    return false
  }
  var a = arrayRow(2);
  if(a.length == 0 || a.length == 1) {
    alertGrowl(MISC_ERROR, VISU_ERROR_EMPTYSELECT);
    return false
  }
  $.post(baseUrl + "/creation/createmap/", {name:name.val()}, checkNameMap, "json");
  return true
}
function addMonitor() {
 if(tabulaireTable == undefined) {
    alertGrowl(MISC_ERROR, VISU_ERROR_EMPTYSEARCH)
 } else {
 var a = arrayRow(2), b = arrayRow(1);
 if(a.length == 0) {
    alertGrowl(MISC_ERROR, VISU_ERROR_ENOUGHTSELECT);
    return false
  }
 $.post(baseUrl + "/dashboard/ajoutermonitor/", {ip:a.toString(),name:b.toString()}, alertGrowl(TOPOLOGI_MONITOR_TXT, TOPOLOGI_MONITOR_ADD), "json");
 }
}
function stopResearch() {
    if (timers.tab['/visualisation/lancerechercheresult'][4] == null) {
        return false;
    }
    alertGrowl(VISU_SEARCH_TXT, VISU_SEARCH_STOP);
    clearTimeout(timers.tab['/visualisation/lancerechercheresult'][4])
    timers.tab['/visualisation/lancerechercheresult'][4] = null
    $.post(baseUrl + "/visualisation/stoprecherche", {}, attenteReponse, "json");
}